﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using BrainTechLLC;
using BrainTechLLC.WPF;
using ImageAppViewModels;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for ImageInfoView.xaml
    /// </summary>
    public partial class ImageInfoView : UserControl
    {
        public DispatcherTimer _dt;
        public BackgroundCalcHandler CalcHandler;

        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register("FileImageViewModel", typeof(FileImageViewModel), typeof(ImageInfoView), new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { SetValue(FileImageViewModelProperty, value); }
        }

        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageInfoView source = d as ImageInfoView;

            FileImageViewModel v = e.NewValue as FileImageViewModel;
            FileImageViewModel vOld = e.OldValue as FileImageViewModel;

            source.UpdateFileImageViewModel(v, vOld);
        }

        public void UpdateFileImageViewModel(FileImageViewModel viewModel, FileImageViewModel viewModelOld)
        {
            if (viewModelOld != null)
            {
                viewModelOld.PropertyChanged -= FileImageViewModel_PropertyChanged;
            }

            if (viewModel != null)
            {
                viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(FileImageViewModel_PropertyChanged);
            }

            // TODO - wait before kicking off the background calc blur
            if (viewModel != viewModelOld)
            {
                if (viewModel != null)
                {
                    if (_dt == null)
                    {
                        _dt = new DispatcherTimer(DispatcherPriority.Background)
                        {
                            Interval = new TimeSpan(0, 0, 0, 0, 400),
                        };
                    }

                    _dt.Tag = FileImageViewModel;
                    _dt.Tick += new EventHandler(dt_Tick);
                    _dt.Start();
                }
            }

            if (viewModel == null)
            {
                CalcHandler.Abort();
            }

            this.DataContext = viewModel;
        }

        void dt_Tick(object sender, EventArgs e)
        {
            _dt.Stop();
            _dt.Tick -= dt_Tick;
            FileImageViewModel vm = _dt.Tag as FileImageViewModel;
            _dt.Tag = null;

            if (FileImageViewModel != null && vm == FileImageViewModel)
            {
                if (view1.IsVisible)
                {
                    CalcHandler.SetBackgroundCalcBlur(vm);
                }
            }
        }

        public static readonly DependencyProperty View1ActiveProperty =
            DependencyProperty.Register("View1Active", typeof(bool), typeof(ImageInfoView), new PropertyMetadata(true, OnView1ActiveChanged));

        public bool View1Active
        {
            get { return (bool)GetValue(View1ActiveProperty); }
            set { SetValue(View1ActiveProperty, value); }
        }

        private static void OnView1ActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageInfoView source = d as ImageInfoView;
            source.UpdateView1Active((bool)e.NewValue);
        }

        public void UpdateView1Active(bool active)
        {
            View1Active = active;
            btnView1.IsChecked = active;
            view1.Visibility = active.GetVisibility();
        }

        public static readonly DependencyProperty View2ActiveProperty =
            DependencyProperty.Register("View2Active", typeof(bool), typeof(ImageInfoView), new PropertyMetadata(false, OnView2ActiveChanged));

        public bool View2Active
        {
            get { return (bool)GetValue(View2ActiveProperty); }
            set { SetValue(View2ActiveProperty, value); }
        }

        private static void OnView2ActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageInfoView source = d as ImageInfoView;
            source.UpdateView2Active((bool)e.NewValue);
        }

        public void UpdateView2Active(bool active)
        {
            View2Active = active;
            btnView2.IsChecked = active;
            view2.Visibility = active.GetVisibility();
        }

        void FileImageViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //Console.WriteLine(e.PropertyName);
        }

        public ImageInfoView()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ImageInfoView_Loaded);
            CalcHandler = new BackgroundCalcHandler(false);
        }

        void ImageInfoView_Loaded(object sender, RoutedEventArgs e)
        {
            View1Active = Settings.Values.ShowFullViewerDetails1;
            View2Active = Settings.Values.ShowFullViewerDetails2;
        }

        private void btnView1_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            stacker.Background = new SolidColorBrush(Colors.WhiteSmoke);
        }

        private void btnView1_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            stacker.Background = null;
        }

        private void btnView1_Checked(object sender, RoutedEventArgs e)
        {
            View1Active = true;
        }

        private void btnView1_Unchecked(object sender, RoutedEventArgs e)
        {
            View1Active = false;
        }

        private void btnView2_Checked(object sender, RoutedEventArgs e)
        {
            View2Active = true;
        }

        private void btnView2_Unchecked(object sender, RoutedEventArgs e)
        {
            View2Active = false;
        }

        private void btnView2_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            stacker2.Background = new SolidColorBrush(Colors.WhiteSmoke);
        }

        private void btnView2_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            stacker2.Background = null;
        }

        public void ClearAll()
        {
            _dt.Stop();
            _dt = null;
            FileImageViewModel = null;
        }
    }
}
